<?php
namespace app\admin\controller;
use think\Db;
use app\app\util\VoteUtils;

class Withdraw extends Common{
    public function index(){
        if(request()->isPost()){
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('withdraw')->alias('u')
                ->field('u.id,u.total_fee,u.order_number,u.pay_time,u.status,u.create_time,ul.nickname')
                ->join(config('database.prefix').'user ul','u.uid = ul.id','left')
                ->where('ul.nickname|u.total_fee|u.order_number','like',"%".$key."%")
                ->where('u.status','neq',0)
                ->where('ul.status','eq',1)
                ->order('u.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            foreach($list['data'] as $k => $v){
                $list['data'][$k]['total_fee'] = $v['total_fee']/100;
                if($v['status'] == 1){
                    $list['data'][$k]['status'] = "未提现";
                }elseif($v['status'] == 2){
                    $list['data'][$k]['status'] = "已提现";
                }else{
                    $list['data'][$k]['status'] = "已销毁";
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }

    //删除留言
    public function withdrawDel(){
        // $map['id']=input('id');
        db('withdraw')->where('id','eq',input('id'))->update(array('status'=>0));
        return $result = ['code'=>1,'msg'=>'删除成功!'];
    }
    public function delall(){
        $map[] =array('id','IN',input('param.ids/a'));
        db('withdraw')->where($map)->update(array('status'=>0));
        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        $result['url'] = url('index');
        return $result;
    }

    //更新提现信息
    public function edit(){
        if(request()->isPost()){
            $data = input('post.');
            $arr['status'] = $data['status'];
            $arr['id'] = $data['id'];
            $arr['over_time'] = date('Y-m-d H:i:s',time());
            $withdraw = db('withdraw');
            if($withdraw->update($arr) !== false){
                $fee = intval($data['total_fee'])*100;
                Db::query('update wc_user set fee = fee -'.$fee.' where id ='.$data['uid']);
                return $result = ['code'=>1,'msg'=>'提现成功!','url'=>url('index')];
            }else{
                return $result = ['code'=>0,'msg'=>'提现失败','url'=>url('index')];
            }
        }else{
            $id = input('id');
            $withdraw = current(Db::query('select a.id,a.total_fee,a.alipay,a.apply_time,a.status,a.over_time,b.nickname,a.uid from wc_withdraw a left join wc_user b on a.uid=b.id where a.id='.$id.' and a.status!=0 and b.status=1'));
            $this->assign('withdraw',$withdraw);
            return view('edit');
        }
    }


}